<template>
  <view>
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>
    <!-- 轮播图 -->
    <swiper circular :indicator-dots="true" :autoplay="true" :interval="3000" :duration="500">
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <navigator hover-class="none" class="swiper-item"
          :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <image :src="item.image_src" mode="widthFix"></image>
        </navigator>
      </swiper-item>

    </swiper>
    <!-- 分类 -->
    <view class="nav-list">
      <view @click="navClickHandler(item.name)" class="nav-item" v-for="(item, i) in navList" :key="i">
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>
    <!-- 楼层 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- 标题 -->
        <img class="floor-title" :src="item.floor_title.image_src" alt="">
        <!-- 图片区域 -->
        <view class="img-box">
          <!-- 左侧图片 -->
          <navigator hover-class="none" :url="item.product_list[0].url" class="img-left">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}"
              mode="widthFix"></image>
          </navigator>
          <!-- 右侧图片 -->
          <view class="img-right">
            <navigator hover-class="none" :url="item2.url" class="right-img-item"
              v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2 !== 0">
              <image :src="item2.image_src" mode="widthFix" :style="{width:item2.image_width + 'rpx'}"></image>
            </navigator>
          </view>

        </view>

      </view>
    </view>
  </view>
</template>

<script>
  // 导入自己封装的 mixin 模块
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [badgeMix],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    methods: {
      // 获取轮播图列表
      async getSwiperList() {
        const { data: res } = await this.$axios.get('/home/swiperdata')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.swiperList = res.message

      },
      // 获取导航栏列表
      async getNavList() {
        const { data: res } = await this.$axios.get('/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message

      },
      // 获取楼层图片列表
      async getFloorList() {
        const { data: res } = await this.$axios.get('/home/floordata')
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      // 分类点击跳转
      navClickHandler(name) {
        if (name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 跳转搜索
      gotoSearch() {
        uni.navigateTo({
          url: "/subpkg/search/search"
        })
      }
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  swiper {
    height: 330rpx;

    .swiper-item {
      image {
        width: 100%;
      }
    }
  }


  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-list {


    .floor-item {

      .floor-title {
        display: flex;
        height: 60rpx;
        width: 100%;
      }

      .img-box {
        display: flex;
        padding-left: 10rpx;
      }

      .img-right {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;

      }
    }
  }
</style>
